import Vuex from "vuex";
import config from "../config";
import {
	apiLogin,
	apiFaceCertify,
	apiFaceQuery,
	apiDbArea
} from "@/api/common";
const store = new Vuex.Store({
	state: {
		token: uni.getStorageSync("token") || "",
		userInfo: uni.getStorageSync("token") ? uni.getStorageSync("userInfo") : {},
		
	},
	mutations: {
		SET_TOKEN(state, value) {
			state.token = value;
		},
		SET_USERINFO(state, value) {
			state.userInfo = value;
		},
		
	},
	actions: {
		// 登录
		ASYNC_LOGIN({
			commit
		}, cb) {
			// #ifdef MP-ALIPAY
			httplogin(cb);
			// #endif
			// 多端接口请求统一处理
			function httplogin(cb) {
				uni.getAuthCode({
					scopes: "auth_user",
					success: (res) => {
						console.log(res, "3要素");
						uni.showLoading({
							mask: true,
						});
						apiLogin({
							authCode: res.authCode,
						}).then((res) => {
							if (res.code === 200) {
								uni.hideLoading({
									mask: true,
								});
								commit("SET_TOKEN", res.data.token);
								commit("SET_USERINFO", res.data.user);
								uni.setStorageSync({
									key: "token",
									data: res.data.token,
								});
								uni.setStorageSync({
									key: "userInfo",
									data: res.data.user,
								});
								cb && cb(res.data.user);
							}
						});
					},
					fail: (err) => {},
				});
			}
		},
		
	},
});
export default store;